package com.yonyou.brigade.third_parties.ctrip.bigcustomer.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.order_center.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderConvert {

    private static Logger logger = LoggerFactory.getLogger(OrderConvert.class);

    public static BriFlightOrder getBriFlightOrderByJSON(JSONObject orderJson) {
        BriFlightOrder briFlightOrder = new BriFlightOrder();
        briFlightOrder.setChannelId(orderJson.getString("channelId"));
        JSONObject basicInfo = orderJson.getJSONObject("BasicInfo");
        JSONArray passengerInfo = orderJson.getJSONArray("PassengerInfo");
        briFlightOrder.setFromType(ChannelEnum.CTRIPBC.getName());
        briFlightOrder.setOrderNo(basicInfo.getString("OrderID"));
        briFlightOrder.setPublicOrPrivate(convertFlightPublicOrPrivate(basicInfo.getString("CorpPayType")));
        briFlightOrder.setIsDomc(convertIsDomc(basicInfo.getString("FlightClass")));
        briFlightOrder.setOrderStatus(convertFlightOrderStatus(basicInfo.getString("OrderStatus")));
        briFlightOrder.setPsgCount(passengerInfo.size());
        briFlightOrder.setPaymentPrice(basicInfo.getBigDecimal("Amount"));//不包含改退签的服务费
        briFlightOrder.setTpPrice(basicInfo.getBigDecimal("RefundAmount"));
        briFlightOrder.setTripType(convertFlightTripType(basicInfo.getString("FlightWay")));
        briFlightOrder.setSumCounterFee(new BigDecimal(basicInfo.getInteger("TotalServiceFee")));
        briFlightOrder.setCreateBy(basicInfo.getString("PreEmployName"));
        briFlightOrder.setOrderTime(basicInfo.getDate("CreateTime"));
        briFlightOrder.setBxStatus(0);//暂时固定值0
        //现在携程没有字段来表明是现付还是预付，故payType目前不赋值
//        briFlightOrder.setPayType(basicInfo.getString("PrepayType"));
        briFlightOrder.setPaymentType(basicInfo.getString("PrepayType"));
        briFlightOrder.setSendTicketFee(basicInfo.getBigDecimal("SendTicketFee"));
        briFlightOrder.setChangeFee(basicInfo.getBigDecimal("ChangeAmount"));
        briFlightOrder.setCurrency(basicInfo.getString("Currency"));
        briFlightOrder.setRelateYbzId(basicInfo.getString("JourneyID"));
        briFlightOrder.setAuthStatus(basicInfo.getString("AuditStatus"));
        briFlightOrder.setSumInsMoney(new BigDecimal(basicInfo.getInteger("InsuranceFee")));
//        String preBookReasonDesc = "";
//        String reasonDesc = "";
//        if (flightInfo != null && flightInfo.size() > 0) {
//            for (int i = 0; i < flightInfo.size(); i++) {
//                JSONObject flightInfoJSONObject = flightInfo.getJSONObject(i);
//                preBookReasonDesc += flightInfoJSONObject.getString("PreBookReasonDesc");
//                reasonDesc += flightInfoJSONObject.getString("ReasonDesc");
//                if (i < (flightInfo.size()-1)) {
//                    preBookReasonDesc += ",";
//                    reasonDesc += ",";
//                }
//                briFlightOrder.setFarePrice(flightInfoJSONObject.getBigDecimal("Price"));
//            }
//        }
//        briFlightOrder.setNoPreOrderReason(preBookReasonDesc);
//        briFlightOrder.setNoOrderLowstReason(reasonDesc);
//        JSONArray passengerInfo = orderJson.getJSONArray("PassengerInfo");
//        if (passengerInfo != null && passengerInfo.size() > 0) {
//            for (int i = 0; i < passengerInfo.size(); i++) {
//                JSONObject flightPassenger = passengerInfo.getJSONObject(i);
//                JSONArray sequenceInfos = flightPassenger.getJSONArray("SequenceInfo");
//                if (sequenceInfos != null && sequenceInfos.size() > 0) {
//                    for (int j = 0; j < sequenceInfos.size(); j++) {
//                        JSONObject sequenceInfosJSONObject = sequenceInfos.getJSONObject(j);
//                        JSONArray changeInfos = sequenceInfosJSONObject.getJSONArray("ChangeInfo");
//                        if (changeInfos != null && changeInfos.size() > 0) {
//                            for (int k = 0; k < changeInfos.size(); k++) {
//                                JSONObject changeInfosJSONObject = changeInfos.getJSONObject(k);
//                                briFlightOrder.setRelatedNo(changeInfosJSONObject.getString("OriTicketNO"));
//                            }
//                        }
//                    }
//                }
//            }
//        }
        return  briFlightOrder;
    }


    public static List<BriFlightInfo> getBriFlightInfoByJSON(JSONObject orderJson, String orderId) throws ParseException {
        List<BriFlightInfo> flightInfos = new ArrayList<>();
        JSONArray flightInfo = orderJson.getJSONArray("FlightInfo");
        if (flightInfo != null && flightInfo.size() > 0) {
            for (int i = 0; i < flightInfo.size(); i++) {
                JSONObject flightInfoJSONObject = flightInfo.getJSONObject(i);
                BriFlightInfo briFlightInfo = new BriFlightInfo();
                briFlightInfo.setOrderNo(orderId);
                briFlightInfo.setFromType(ChannelEnum.CTRIPBC.getName());
                briFlightInfo.setCarrierName(flightInfoJSONObject.getString("AirLineName"));
                briFlightInfo.setBoardPoint(flightInfoJSONObject.getString("DCityCode"));
                briFlightInfo.setBoardPointName(flightInfoJSONObject.getString("DCityName"));
                briFlightInfo.setBoardPointAirPortName(flightInfoJSONObject.getString("DPortName"));
                if (flightInfoJSONObject.getJSONObject("DAirport") != null) {
                    briFlightInfo.setBoardPointAt(flightInfoJSONObject.getJSONObject("DAirport").getString("Name"));
                }
                //起飞日期，时间和到达日期，时间
                briFlightInfo.setDepartureDate(getDateFromDateTime(flightInfoJSONObject.getString("TakeoffTime")));
                briFlightInfo.setDepartureTime(getTimeFromDateTime(flightInfoJSONObject.getString("TakeoffTime")));
                briFlightInfo.setArriveDate(getDateFromDateTime(flightInfoJSONObject.getString("ArrivalTime")));
                briFlightInfo.setArriveTime(getTimeFromDateTime(flightInfoJSONObject.getString("ArrivalTime")));
                briFlightInfo.setOffPoint(flightInfoJSONObject.getString("ACityCode"));
                briFlightInfo.setOffPointName(flightInfoJSONObject.getString("ACityName"));
                briFlightInfo.setOffPointAirPortName(flightInfoJSONObject.getString("APortName"));
                briFlightInfo.setOffPointAt(flightInfoJSONObject.getJSONObject("AAirport").getString("Name"));
                briFlightInfo.setFlightNo(flightInfoJSONObject.getString("Flight"));
                if (flightInfoJSONObject.getInteger("FlightTime") != null) {
                    briFlightInfo.setTimeSpan(String.valueOf(flightInfoJSONObject.getInteger("FlightTime")/60)+" 小时 "+String.valueOf(flightInfoJSONObject.getInteger("FlightTime")%60)+" 分");
                }
                briFlightInfo.setYprice(flightInfoJSONObject.getString("StandardPrice"));
                briFlightInfo.setClassLevelName(flightInfoJSONObject.getString("ClassName"));
                briFlightInfo.setLowestPrice((flightInfoJSONObject.getBigDecimal("LowestPrice")));
                if (flightInfoJSONObject.getBigDecimal("PriceRate") != null) {
                    briFlightInfo.setClassRebate(String.valueOf(flightInfoJSONObject.getBigDecimal("PriceRate").multiply(new BigDecimal(100))));
                }
                flightInfos.add(briFlightInfo);
            }
        }
        return flightInfos;
    }

    public static List<BriFlightPassenger> getBriFlightPassengerByJSON(JSONObject orderJson, String orderId) {
        List<BriFlightPassenger> passengers = new ArrayList<>();
        JSONArray passengerInfo = orderJson.getJSONArray("PassengerInfo");
        if (passengerInfo != null && passengerInfo.size() > 0) {
            for (int i = 0; i < passengerInfo.size(); i++) {
                JSONObject flightPassenger = passengerInfo.getJSONObject(i);
                BriFlightPassenger briFlightPassenger = new BriFlightPassenger();
                JSONObject passengerBasic = flightPassenger.getJSONObject("PassengerBasic");
                briFlightPassenger.setOrderNo(orderId);
                briFlightPassenger.setPsgName(passengerBasic.getString("PassengerName"));
                briFlightPassenger.setOAUserCode(passengerBasic.getString("CorpEid"));
                JSONArray flightInfo = orderJson.getJSONArray("FlightInfo");
                briFlightPassenger.setPsgType(convertFlightPsgType(flightInfo.getJSONObject(0).getString("AgeType")));
                briFlightPassenger.setCardType(passengerBasic.getString("CardTypeName"));
                briFlightPassenger.setCardNo(passengerBasic.getString("CardTypeNumber"));
                briFlightPassenger.setDepartment1(passengerBasic.getString("Dept1"));
                briFlightPassenger.setDepartment2(passengerBasic.getString("Dept2"));
                briFlightPassenger.setDepartment3(passengerBasic.getString("Dept3"));
                briFlightPassenger.setCostCenter(passengerBasic.getString("CostCenter"));
                briFlightPassenger.setCostCenter2(passengerBasic.getString("CostCenter2"));
                briFlightPassenger.setCostCenter3(passengerBasic.getString("CostCenter3"));
//                briFlightPassenger.setConsturctAndFuel(passengerBasic.getBigDecimal("Tax").add(passengerBasic.getBigDecimal("OilFee")));
//                briFlightPassenger.setCounterFee(passengerBasic.getBigDecimal("ServerFee"));
                JSONArray sequenceInfos = flightPassenger.getJSONArray("SequenceInfo");
                if (sequenceInfos != null && sequenceInfos.size() > 0) {
                    if (sequenceInfos.size() == 1) {
                        //单程，一个票号
                        JSONObject sequenceInfosJSONObject = sequenceInfos.getJSONObject(0);
                        JSONArray ticketInfos = sequenceInfosJSONObject.getJSONArray("TicketInfo");
                        if (ticketInfos != null && ticketInfos.size() > 0) {
                            JSONObject ticketInfosJSONObject = ticketInfos.getJSONObject(0);
                            if ("2".equals(ticketInfosJSONObject.getString("Status"))) {
                                briFlightPassenger.setTicketNo(ticketInfosJSONObject.getString("TicketNo"));
                            }
                        }
                    } else if (sequenceInfos.size() == 2) {
                        //往返，两个票号，根据顺序存
                        JSONObject sequenceInfosJSONObject = sequenceInfos.getJSONObject(0);
                        JSONArray ticketInfos = sequenceInfosJSONObject.getJSONArray("TicketInfo");
                        if (ticketInfos != null && ticketInfos.size() > 0) {
                            for (int j = 0; j < ticketInfos.size(); j++) {
                                JSONObject ticketInfosJSONObject = ticketInfos.getJSONObject(j);
                                if ("2".equals(ticketInfosJSONObject.getString("Status"))) {
                                    briFlightPassenger.setTicketNo(ticketInfosJSONObject.getString("TicketNo"));
                                }
                            }
                        }
                        JSONObject sequenceInfosJSONObjectReturn = sequenceInfos.getJSONObject(1);
                        JSONArray ticketInfosReturn = sequenceInfosJSONObjectReturn.getJSONArray("TicketInfo");
                        if (ticketInfosReturn != null && ticketInfosReturn.size() > 0) {
                            for (int j = 0; j < ticketInfosReturn.size(); j++) {
                                JSONObject ticketInfosJSONObject = ticketInfosReturn.getJSONObject(j);
                                if ("2".equals(ticketInfosJSONObject.getString("Status"))) {
                                    briFlightPassenger.setTicketNoT(ticketInfosJSONObject.getString("TicketNo"));
                                }
                            }
                        }
                    }
                }
                passengers.add(briFlightPassenger);
            }
        }
        return passengers;
    }

    public static BriHotelOrder getBriHotelOrderByJSON(JSONObject orderJson) throws ParseException {
        BriHotelOrder hotelOrder = new BriHotelOrder();
        hotelOrder.setOrderNo(orderJson.getString("OrderID"));
        hotelOrder.setIsDomc(convertIsDomc(orderJson.getString("IsDomestic")));
        // 中航易购给的是rtpnr_user的f_login_id,携程给的是人名
        hotelOrder.setCreateBy(orderJson.getString("PreEmployeeName"));
        hotelOrder.setOrderTime(convertDateFromString(orderJson.getString("OrderDate")));
        hotelOrder.setCreateTime(convertDateFromString(orderJson.getString("OrderDate")));
        hotelOrder.setOrderStatus(convertHotelOrderStatus(orderJson.getString("OrderStatus")));
        hotelOrder.setOrderStatusName(convertHotelOrderStatusName(orderJson.getString("OrderStatus")));
        hotelOrder.setPaymentPrice(orderJson.getBigDecimal("CustomPayAmount"));
        hotelOrder.setHotelName(orderJson.getString("HotelName"));
        hotelOrder.setRoomNums(orderJson.getInteger("RoomQuantity"));
        hotelOrder.setArrivalDate(orderJson.getDate("StartTime"));
        hotelOrder.setDepartureDate(orderJson.getDate("EndTime"));
        hotelOrder.setHotelAddress(orderJson.getString("Address"));
        hotelOrder.setHotelTel(orderJson.getString("Telephone"));
        hotelOrder.setPayType(convertHotelPayType(orderJson.getString("BalanceType")));
        hotelOrder.setContactName(orderJson.getString("ContactName"));
        hotelOrder.setContactMobile(orderJson.getString("ContactTel"));
        hotelOrder.setCancelTime(orderJson.getDate("LastCancelTime"));
        hotelOrder.setGcjLat(BigDecimal.valueOf(orderJson.getDouble("GDLat")));
        hotelOrder.setGcjLon(BigDecimal.valueOf(orderJson.getDouble("GDLon")));
        //中航易购为prepay selfpay   携程为 现金（备注：即前台现付）；公司账户；礼品卡支付；信用卡支付
        hotelOrder.setPaymentType(convertHotelPaymentType(orderJson.getString("PayType")));
        hotelOrder.setFromType(ChannelEnum.CTRIPBC.getName());
        hotelOrder.setCardHolderName(orderJson.getString("PreEmployeeName"));
        hotelOrder.setHotelType(orderJson.getString("HotelType"));
        hotelOrder.setHotelCity(orderJson.getString("CityName"));
        hotelOrder.setHotelStars(String.valueOf(orderJson.getInteger("Star")));
        hotelOrder.setInNights(orderJson.getInteger("RoomDays"));
        if (orderJson.getInteger("RoomQuantity") != 0) {
            hotelOrder.setInDays(orderJson.getInteger("RoomDays")/(orderJson.getInteger("RoomQuantity")));
        }
        hotelOrder.setCurrency(orderJson.getString("CustomPayCurrency"));
        hotelOrder.setRelateYBZID(orderJson.getString("JourneyNo"));
        hotelOrder.setAuthStatus(orderJson.getString("AuthorizeStatus"));
        hotelOrder.setChannelId(orderJson.getString("channelId"));
        return hotelOrder;
    }

    public static List<BriHotelRoom> getBriHotelRoomByJSON(JSONObject orderJson, String orderId) {
        List<BriHotelRoom> hotelRooms = new ArrayList<>();
        JSONArray clientInfos = orderJson.getJSONArray("ClientInfo");
        JSONArray roomInfos = orderJson.getJSONArray("RoomInfo");
        if (clientInfos != null && clientInfos.size() > 0 && roomInfos != null && roomInfos.size() > 0) {
            for (int i = 0; i < clientInfos.size(); i++) {
                BriHotelRoom briHotelRoom = new BriHotelRoom();
                JSONObject clientInfosJSONObject = clientInfos.getJSONObject(i);
                briHotelRoom.setOrderNO(orderId);
                briHotelRoom.setPsgName(clientInfosJSONObject.getString("ClientName"));
                briHotelRoom.setOAUserCode(clientInfosJSONObject.getString("EmployeeID"));
                briHotelRoom.setCostCenter(clientInfosJSONObject.getString("CostCenter1"));
                briHotelRoom.setCostCenter2(clientInfosJSONObject.getString("CostCenter2"));
                briHotelRoom.setCostCenter3(clientInfosJSONObject.getString("CostCenter3"));
                JSONObject roomInfosJSONObject = roomInfos.getJSONObject(0);
                briHotelRoom.setRoomName(roomInfosJSONObject.getString("RoomName"));
                // 不知道具体有哪些房型编号
                briHotelRoom.setRoomTypeId(String.valueOf(roomInfosJSONObject.getInteger("Room")));
                // ? 携程拉过来的人数是订单中的总人数，而中航易购的人数是每个酒店房间的人数，所以没存
                // briHotelRoom.setPsgNums(clientInfos.size());
                hotelRooms.add(briHotelRoom);
            }
        }
        return hotelRooms;
    }

    public static BriTrainOrder getBriTrainOrderByJSON(JSONObject orderJson) {
        BriTrainOrder briTrainOrder = new BriTrainOrder();
        briTrainOrder.setChannelId(orderJson.getString("channelId"));
        JSONObject basicInfo = orderJson.getJSONObject("BasicInfo");
        JSONObject corpOrderInfo = orderJson.getJSONObject("CorpOrderInfo");
        briTrainOrder.setTicketsNum(orderJson.getJSONArray("TicketInfoList").size());
        briTrainOrder.setOrderNo(basicInfo.getString("OrderID"));
        briTrainOrder.setFromType(ChannelEnum.CTRIPBC.getName());
        briTrainOrder.setPublicOrPriveate(convertTrainPublicOrPriveate(corpOrderInfo.getString("CorpPayType")));
        briTrainOrder.setCreateBy(basicInfo.getString("UserName"));
        briTrainOrder.setOrderTime(basicInfo.getDate("DataChange_CreateTime"));
        briTrainOrder.setOrderStatus(convertTrainOrderStatus(basicInfo.getString("OrderStatus")));
        briTrainOrder.setLinkName(basicInfo.getString("ContactName"));
        briTrainOrder.setLinkPhone(basicInfo.getString("ContactMobile"));
        briTrainOrder.setSumCounterFee(basicInfo.getBigDecimal("ServiceFee"));
        briTrainOrder.setCarrierSumFee(basicInfo.getBigDecimal("ChangeServiceFee"));
        briTrainOrder.setOrderStatusName(basicInfo.getString("OrderStatusName"));
        briTrainOrder.setRelateYBZID(corpOrderInfo.getString("JouneryID"));
        briTrainOrder.setAuthStatus(basicInfo.getString("AuditResult"));
        briTrainOrder.setPaymentPrice(basicInfo.getBigDecimal("DealAmount"));
        briTrainOrder.setPaymentType(basicInfo.getString("PaymentType"));
        briTrainOrder.setPaperTicketFee(basicInfo.getBigDecimal("PaperTicketFee"));
        return briTrainOrder;
    }

    public static List<BriTrainPassenger> getBriTrainPassngerByJSON(JSONObject orderJson, String orderId) {
        List<BriTrainPassenger> trainPassengers = new ArrayList<>();
        JSONArray passengerInfos = orderJson.getJSONArray("PassengerInfoList");
        if (passengerInfos != null && passengerInfos.size() > 0) {
            for (int i = 0; i < passengerInfos.size(); i++) {
                JSONObject passengerInfosJSONObject = passengerInfos.getJSONObject(i);
                BriTrainPassenger briTrainPassenger = new BriTrainPassenger();
                briTrainPassenger.setOrderNo(orderId);
                briTrainPassenger.setPsgName(passengerInfosJSONObject.getString("TicketPassengerName"));
                briTrainPassenger.setOAUserCode(passengerInfosJSONObject.getString("EmployeeID"));
                briTrainPassenger.setTicketType(convertTicketTypeName(passengerInfosJSONObject.getString("TicketTypeName")));
                briTrainPassenger.setCardType(passengerInfosJSONObject.getString("IdentityTypeName"));
                briTrainPassenger.setCardNo(passengerInfosJSONObject.getString("IdentityNo"));
                briTrainPassenger.setCostCenter(passengerInfosJSONObject.getString("CostCenter1"));
                briTrainPassenger.setCostCenter2(passengerInfosJSONObject.getString("CostCenter2"));
                briTrainPassenger.setCostCenter3(passengerInfosJSONObject.getString("CostCenter3"));
                briTrainPassenger.setPsgNo(String.valueOf(passengerInfosJSONObject.getInteger("PassengerID")));
                briTrainPassenger.setSumInsMoney(passengerInfosJSONObject.getBigDecimal("InsuranceUnitPrice"));
                briTrainPassenger.setDepartment1(passengerInfosJSONObject.getString("Dept1"));
                briTrainPassenger.setDepartment2(passengerInfosJSONObject.getString("Dept2"));
                briTrainPassenger.setDepartment3(passengerInfosJSONObject.getString("Dept3"));
                trainPassengers.add(briTrainPassenger);
            }
        }
        return trainPassengers;
    }

    public static List<BriTrainTicket> getBriTrainTicketByJSON(JSONObject orderJson, String cardNo, String orderId) throws ParseException {
        List<BriTrainTicket> trainTickets = new ArrayList<>();
        JSONArray ticketInfos = orderJson.getJSONArray("TicketInfoList");
        JSONArray passengerInfos = orderJson.getJSONArray("PassengerInfoList");
        //获取所有票的状态，以便查看是否有改签
        List<String> trainTicketTypes = new ArrayList<>();
        String trainTicketType = null;
        if (ticketInfos != null && ticketInfos.size() > 0) {
            for (int i = 0; i < ticketInfos.size(); i++) {
                JSONObject ticketInfosJSONObject = ticketInfos.getJSONObject(i);
                trainTicketType = ticketInfosJSONObject.getString("TrainTicketType");
                trainTicketTypes.add(trainTicketType);
            }
        }
        //判断是否有改签票，若有则只保存改签后的票信息，如没有改签，则保存所有票信息(暂定)
        if (ticketInfos != null && ticketInfos.size() > 0 && trainTicketTypes.contains("C")) {
            for (int i = 0; i < ticketInfos.size(); i++) {
                JSONObject ticketInfosJSONObject = ticketInfos.getJSONObject(i);
                trainTicketType = ticketInfosJSONObject.getString("TrainTicketType");
                if ("C".equals(trainTicketType)) {
                    BriTrainTicket briTrainTicket = convertTicketFromJSON(cardNo, orderId, ticketInfosJSONObject, passengerInfos);
                    trainTickets.add(briTrainTicket);
                }
            }
        } else if (trainTicketTypes != null && ticketInfos.size() > 0 && !trainTicketTypes.contains("C")) {
            for (int i = 0; i < ticketInfos.size(); i++) {
                JSONObject ticketInfosJSONObject = ticketInfos.getJSONObject(i);
                BriTrainTicket briTrainTicket = convertTicketFromJSON(cardNo, orderId, ticketInfosJSONObject, passengerInfos);
                trainTickets.add(briTrainTicket);
            }
        }
        return trainTickets;
    }

    public static List<BriTrainPayment> getBriTrainPaymentByJSON(JSONObject orderJson, String orderId) {
        List<BriTrainPayment> briTrainPayments = new ArrayList<>();
        JSONArray paymentInfos = orderJson.getJSONArray("PaymentInfoList");
        if (paymentInfos != null && paymentInfos.size() > 0) {
            for (int i = 0; i < paymentInfos.size(); i++) {
                JSONObject paymentInfosJSONObject = paymentInfos.getJSONObject(i);
                BriTrainPayment briTrainPayment = new BriTrainPayment();
                briTrainPayment.setOrderNo(orderId);
                briTrainPayment.setCurrency(paymentInfosJSONObject.getString("Currency"));
                briTrainPayment.setAmount(paymentInfosJSONObject.getBigDecimal("Amount"));
                briTrainPayment.setExchangeRate(paymentInfosJSONObject.getBigDecimal("ExchangeRate"));
                briTrainPayment.setDealType(paymentInfosJSONObject.getString("DealType"));
                briTrainPayment.setPayType(paymentInfosJSONObject.getString("PayWayID"));
                briTrainPayment.setPayRemark(paymentInfosJSONObject.getString("PayRemark"));
                briTrainPayment.setVoucherPayAmount(paymentInfosJSONObject.getBigDecimal("VoucherPayAmount"));
                briTrainPayment.setPayCategory(paymentInfosJSONObject.getString("PayCategory"));
                briTrainPayments.add(briTrainPayment);
            }
        }
        return briTrainPayments;
    }

    public static BriTrainTicket convertTicketFromJSON (String cardNo, String orderId, JSONObject ticketInfosJSONObject, JSONArray passengerInfos) throws ParseException {
        BriTrainTicket briTrainTicket = new BriTrainTicket();
        briTrainTicket.setCardNo(cardNo);
        briTrainTicket.setOrderNo(orderId);
        briTrainTicket.setTrainCode(ticketInfosJSONObject.getString("TrainName"));
        briTrainTicket.setTicketInfoId(ticketInfosJSONObject.getString("TicketInfoID"));
        briTrainTicket.setTrainCodeName(getTrainCodeNameByTrainCode(ticketInfosJSONObject.getString("TrainName")));
        briTrainTicket.setFromStation(ticketInfosJSONObject.getString("DepartureStationName"));
        briTrainTicket.setToStation(ticketInfosJSONObject.getString("ArrivalStationName"));
        briTrainTicket.setTrainDate(getDateFromDateTime(ticketInfosJSONObject.getString("DepartureDateTime")));
        briTrainTicket.setFromTime(getTimeFromDateTime(ticketInfosJSONObject.getString("DepartureDateTime")));
        briTrainTicket.setToDate(getDateFromDateTime(ticketInfosJSONObject.getString("ArrivalDateTime")));
        briTrainTicket.setToTime(getTimeFromDateTime(ticketInfosJSONObject.getString("ArrivalDateTime")));
        briTrainTicket.setSendTicketFee(ticketInfosJSONObject.getBigDecimal("DeliverFee"));
        //计算时长
        long departureDateTime = ticketInfosJSONObject.getDate("DepartureDateTime").getTime();
        long arrivalDateTime = ticketInfosJSONObject.getDate("ArrivalDateTime").getTime();
        briTrainTicket.setTunTime(String.valueOf((arrivalDateTime - departureDateTime) / 1000 / 60));
        briTrainTicket.setTicketInfoId(ticketInfosJSONObject.getString("TicketInfoID"));
        briTrainTicket.setTrainOrderNo(ticketInfosJSONObject.getString("ElectronicOrderNo"));
        briTrainTicket.setTktPrice(ticketInfosJSONObject.getBigDecimal("TicketPrice"));
        briTrainTicket.setTicketType(ticketInfosJSONObject.getString("TrainTicketType"));
        for (int j = 0; j < passengerInfos.size(); j++) {
            JSONObject passengerInfosJSONObject = passengerInfos.getJSONObject(j);
            JSONArray orderTickets = passengerInfosJSONObject.getJSONArray("OrderTicket");
            if (orderTickets != null && orderTickets.size() > 0) {
                for (int k = 0; k < orderTickets.size(); k++) {
                    JSONObject orderTicketsJSONObject = orderTickets.getJSONObject(k);
                    Long ticketInfoIDOrderTicket = orderTicketsJSONObject.getLong("TicketInfoID");
                    Long ticketInfoIDTicketInfo = ticketInfosJSONObject.getLong("TicketInfoID");
                    if (ticketInfoIDTicketInfo != null && ticketInfoIDTicketInfo.equals(ticketInfoIDOrderTicket)) {
                        briTrainTicket.setSeatType(orderTicketsJSONObject.getString("DealSeatNo"));
                        briTrainTicket.setIsAllowRefund(convertIsAllowRefund(orderTicketsJSONObject.getString("EnableRefundTicket")));
                        briTrainTicket.setRefundAmount(orderTicketsJSONObject.getBigDecimal("Refund"));
                        briTrainTicket.setTpPrice(orderTicketsJSONObject.getBigDecimal("RefundTicketFee"));
                        briTrainTicket.setCounterFee(orderTicketsJSONObject.getBigDecimal("DealTicketServiceFee"));
                        briTrainTicket.setCarrierChangeFee(orderTicketsJSONObject.getBigDecimal("DealChangeServiceFee"));
                        briTrainTicket.setRefundTicketStatus(orderTicketsJSONObject.getString("RefundTicketStatus"));
                        briTrainTicket.setRefundMark(orderTicketsJSONObject.getString("RefundRemark"));
                        briTrainTicket.setChangeStatus(orderTicketsJSONObject.getString("ChangeStatus"));
                        //拆分得到车厢，座位号
                        String dealSeatName = orderTicketsJSONObject.getString("DealSeatName");
                        int trainBoxIndex = dealSeatName.indexOf("车厢");
                        String trainBox = dealSeatName.substring(0, trainBoxIndex + 2);
                        String seatNo = dealSeatName.substring(trainBoxIndex + 2);
                        briTrainTicket.setTrainBox(trainBox);
                        briTrainTicket.setSeatNo(seatNo);
                    }
                }
            }
        }
        return briTrainTicket;
    }

    private static String convertFlightPsgType(String ageType) {
        String result = ageType;
        switch (ageType) {
            case "ADU":
                result = "ADT";
                break;
            case "CHI":
                result = "CHD";
                break;
            case "BAB":
                result = "INF";
                break;
            default:
                break;
        }
        return result;
    }

    private static String convertTrainOrderStatus(String orderStatus) {
        String result = orderStatus;
        switch (orderStatus) {
            case "WA":
                result = "WU";
                break;
            case "AR":
                result = "UR";
                break;
            case "TP":
                result = "TI";
                break;
            case "C":
                result = "CO";
                break;
            default:
                break;
        }
        return result;
    }

    public static String convertTrainPublicOrPriveate(String publicOrPriveate) {
        String result = publicOrPriveate;
        switch (publicOrPriveate) {
            case "C":
                result = "1";
                break;
            case "P":
                result = "2";
                break;
            default:
                break;
        }
        return result;
    }

    public static String getTrainCodeNameByTrainCode (String trainCode) {
        String beginLetter = trainCode.substring(0, 1);
        String result = "";
        switch (beginLetter) {
            case "G":
                result = "高铁";
                break;
            case "D":
                result = "动车";
                break;
            case "T":
                result = "特快";
                break;
            case "Z":
                result = "直达";
                break;
            case "K":
                result = "普快";
                break;
            default:
                break;
        }
        return result;
    }

    public static String convertIsAllowRefund (String isAllowRefund) {
        String result = "";
        switch (isAllowRefund) {
            case "true":
                result = "Y";
                break;
            case "false":
                result = "N";
                break;
            default:
                break;
        }
        return result;
    }

    public static Integer convertTicketTypeName (String ticketTypeName) {
        Integer result = 0;
        switch (ticketTypeName) {
            case "成人票":
                result = 0;
                break;
            case "儿童票":
                result = 1;
                break;
            default:
                break;
        }
        return result;
    }

    public static Time getTimeFromDateTime(String dateTime) throws ParseException {
        Time time = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (dateTime != null) {
                time = new Time(format.parse(dateTime).getTime());
            }
        } catch (ParseException e) {
            logger.error("OrderConvert中时间拆分获取时间出现错误！",e);
            throw e;
        }
        return time;
    }

    public static Date getDateFromDateTime(String dateTime) throws ParseException {
        Date date = null;
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (dateTime != null) {
                date = formatDate.parse(dateTime);
            }
        } catch (ParseException e) {
            logger.error("OrderConvert中时间拆分获取日期出现错误！",e);
            throw e;
        }
        return date;
    }

    public static Date convertDateFromString (String dateTime) throws ParseException {
        Date parse = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (dateTime != null) {
                parse = format.parse(dateTime);
            }
        } catch (ParseException e) {
            logger.error("OrderConvert中字符串转时间类型时出现错误！",e);
            throw e;
        }
        return parse;
    }

    public static String convertFlightPublicOrPrivate(String publicOrPrivate) {
        String result = publicOrPrivate;
        switch (publicOrPrivate) {
            case "因公":
                result = "1";
                break;
            case "因私":
                result = "2";
                break;
            default:
                break;
        }
        return result;
    }

    public static String convertFlightOrderStatus(String orderStatus) {
        String result = orderStatus;
        switch (orderStatus) {
            case "未处理":
                result = "WC";
                break;
            case "处理中":
                result = "CL";
                break;
            case "已取消":
                result = "QX";
                break;
            case "全部退票":
                result = "QT";
                break;
            case "部分退票":
                result = "BT";
                break;
            case "已成交":
                result = "CJ";
                break;
            default:
                break;
        }
        return result;
    }

    public static String convertFlightTripType(String tripType) {
        String result = tripType;
        switch (tripType) {
            case "单程":
                result = "OW";
                break;
            case "往返":
                result = "RT";
                break;
            case "联程":
                result = "RW";
                break;
            case "无航程类型":
                result = "NW";
                break;
            default:
                break;
        }
        return result;
    }

    private static String convertIsDomc(String IsDomestic){
        String result = IsDomestic;
        switch (IsDomestic) {
            case "N":
                result = "D";
                break;
            case  "true":
                result = "D";
                break;
            case "false":
                result = "I";
                break;
            default:
                break;

        }
        return  result;
    }

    private static String convertHotelOrderStatus(String OrderStatus){
        String result = OrderStatus;
        switch (OrderStatus) {
            case "确认中":
                result = "10";
                break;
            case "已确认":
                result = "12";
                break;
            case "已付款":
                result = "17";
                break;
            case "已成交":
                result = "25";
               break;
            case "已取消":
                result = "18";
                break;
            case "未提交":
                result = "26";
               break;
            default:
                break;
        }
        return  result;
    }

    private static String convertHotelOrderStatusName(String OrderStatus){
        String result = OrderStatus;
        switch (OrderStatus) {
            case "确认中":
                result = "待确认";
                break;
            case "已确认":
                result = "已确认(现付)/待支付(预付)";
                break;
            case "已付款":
                result = "已支付";
                break;
            //case "已成交":
            //    result = "I";
            //   break;
            case "已取消":
                result = "变更取消";
                break;
            //case "未提交":
            //    result = "";
            //   break;
            default:
                break;
        }
        return  result;
    }

    private static String convertHotelPayType(String BalanceType){
        String result = BalanceType;
        switch (BalanceType) {
            case "Pay at check-out":
                result = "Prepay";
                break;
            case "Prepay to Ctrip":
                result = "Selfpay";
                break;
            default:
                break;
        }
        return  result;
    }

    private static String convertHotelPaymentType(String PayType){
        String result = PayType;
        switch (PayType) {
            case "信用卡支付":
                result = "CCARD";
                break;
            case "现金":
                result = "CASH";
                break;
            case "礼品卡支付":
                result = "TMPAY";
                break;
            case "公司账户":
                result = "ACCNT";
                break;
            default:
                break;
        }
        return  result;
    }
}
